package cn.itcast.filter;

import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.http.HttpServletRequest;

/**
 * @Author: zhaojie
 * @Date: Created in 10:23 2018/8/1
 * @Description: zuul的过滤器 shouldFilter必须要配置成true才会走
 * run()是过滤器具体的逻辑
 * order() 是在多个过滤器的情况下执行的顺序  数字越大执行的越靠后
 */
public class PreZuulFilter extends ZuulFilter {
    private static final Logger LOGGER=LoggerFactory.getLogger(PreZuulFilter.class);
    @Override
    public String filterType() {
        //前置过滤器
        return "pre";
    }

    /**
     * 过滤器的优先级 越高表示越靠后
     * @return
     */
    @Override
    public int filterOrder() {
        return 0;
    }

    @Override
    public boolean shouldFilter() {
        return true;
    }

    /**
     * 具体的实现逻辑
     * @return
     */
    @Override
    public Object run() {
        //获取servletRequest
        HttpServletRequest request = RequestContext.getCurrentContext().getRequest();
        //想请求哪里
        request.getRemoteHost();
        PreZuulFilter.LOGGER.info("请求的host:"+request.getRemoteHost());
        return null;
    }
}
